package com.zbkj.service.util;

import com.zbkj.dal.pojo.dto.AbstractUserDto;
import com.zbkj.dal.entity.user.User;
import com.zbkj.dal.common.utils.SpringUtil;
import com.zbkj.service.service.UserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户通用操作
 */
public class UserUtils {

    /**
     * 填充用户常用字段
     *
     * @param dtos
     */
    public static <T extends AbstractUserDto> void fillUsers(List<T> dtos) {
        if (CollectionUtils.isEmpty(dtos)) {
            return;
        }
        UserService userService = SpringUtil.getBean(UserService.class);
        List<Integer> userIds = dtos.stream().map(AbstractUserDto::getUid).distinct().collect(Collectors.toList());
        Map<Integer, String> map = userService.listByIds(userIds).stream().collect(Collectors.toMap(User::getId, User::getNickname));
        dtos.forEach(it -> it.setNickName(map.getOrDefault(it.getUid(), StringUtils.EMPTY)));
    }
}
